<?php
 class Upload{
     private $name;     //$_FILE的名字
     private $prefix;   //表前缀
     private $table;    //表
     private $suffix;   //文件后缀名
     private $new_name;   //文件后缀名(没后缀)
     private $path;     //最后的文件名 + 后缀
     private $filedir;  //下载到的主目录
     private $uploads;  //下载到的主目录下的分类目录
     private static $getinstance;

     private function __construct($conf){
         $this->init($conf);
         $this->judge();
         $this->existdir();
         $this->getType();
         $this->setName();
     }

     private function init($conf){
         $this->name = empty($conf['name']) ? '' : $conf['name'];
         $this->prefix = empty($conf['prefix']) ? '' : $conf['prefix'];
         $this->table = empty($conf['table']) ? '' : $conf['table'];
         $this->filedir = empty($conf['filedir']) ? 'upload' : $conf['filedir'];
     }

     private function __clone(){

     }

    static function getInstance($conf){
         if(!self::$getinstance instanceof self){
             return self::$getinstance = new self($conf);
         }
         return self::$getinstance;
     }

     private function judge(){
         // 1. 判断错误信息
         if ($_FILES[$this->name]['error'] > 0) {
             switch ($_FILES[$this->name]['error']) {
                 case 1:
                     echo "文件大小超出了 upload_max_filesize 的值";
                     return;
                 case 2:
                     echo "上传的文件大小超出了MAX_FILE_SIZE指令的值";
                     return;
                 case 3:
                     echo "如果文件没有完全上传";
                     return;
                 case 4:
                     echo "没有指定上传文件";
                     return;
                 default:
                     echo "未知错误";
                     return;
             }
         }
     }

     private function existdir(){
         $this->uploads = $this->filedir . '/' . $this->prefix . $this->table; //指定上传目录
         if (!is_dir($this->uploads)) {
             mkdir($this->uploads, 0777, TRUE);
         }
     }

     private function getType(){
         // 获取文件类型
         $type = explode('/', $_FILES[$this->name]['type']); // image/jpeg

         // 获取文件后缀
         $this->suffix = array_pop($type);

         // 允许上传的数据类型
         $allows = ['jpeg', 'jpg', 'png', 'gif', 'psd'];

         //判断上传的文件类型
         if (!in_array($this->suffix, $allows)) { //in_array检查数组中是否存在某个值
             return "不允许上传 {$this->suffix} 文件类型<br />";
         }
     }

     private function setName(){
         // 指定文件名
         $this->new_name = date("YmdHis") . mt_rand(100, 999) . '.' . $this->suffix;
         $this->path = $this->uploads . '/' . $this->new_name;
     }

     function downLoad(){
         if (move_uploaded_file($_FILES[$this->name]['tmp_name'], $this->path)) {
             $up_info['code'] = 1;
             $up_info['path'] = $this->path;           //图片所在路径 + 文件名
             $up_info['dir'] = $this->uploads;         //所在的文件夹
             $up_info['new_name'] = $this->new_name;   //文件名
             $up_info['msg'] = "上传图片成功!";
         } else {
             $up_info['code'] = 0;
             $up_info['msg'] = "上传图片失败!";
         }
         return $up_info;
     }
 }